<?php
/**
 *+------------------
 * Lflow
 *+------------------
 * Copyright (c) 2023~2030 gitee.com/liu_guan_qing All rights reserved.本版权不可删除，侵权必究
 *+------------------
 * Author: Mr.April(405784684@qq.com)
 *+------------------
 */
declare (strict_types=1);

namespace lflow\implement;

use lflow\Expression;
use lflow\helper\JsonHelper;
use RulerZ\Compiler\Compiler;
use RulerZ\RulerZ;
use RulerZ\Target;

/**
 * 表达式引擎实现
 *
 * @author Mr.April
 * @since  1.0
 */
class JuelExpression implements Expression
{

    /**
     * @param string $expression 表达式
     * @param string $args       参数
     *
     * @return bool
     */
    public function eval(string $expression, string $args): bool
    {
        try {
            $args     = JsonHelper::fromJson($args, true);
            $compiler = Compiler::create();
            $rulerZ   = new RulerZ($compiler, [new Target\Native\Native(['length' => 'strlen'])]);
            $result   = $rulerZ->satisfies($args, $expression);
            return $result ?? false;
        } catch (\Exception $e) {
            return false;
        }
    }
}
